/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: Main5
 * Author:   wanglm
 * Date:     2020/06/01 11:32
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.app.form;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.jb2011.lnf.beautyeye.utils.Platform;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author wanglm
 * @create 2020/06/01
 * @since 1.0.0
 */
public class Main5 extends JFrame {

    private static void initUserInterface() {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        final String s = System.setProperty(
                "com.apple.mrj.application.apple.menu.about.name",
                "Draw 9-patch");

        try {
            if(Platform.isWindows())
            {
                UIManager.put("RootPane.setupButtonVisible", false);
                BeautyEyeLNFHelper.translucencyAtFrameInactive = false;
                BeautyEyeLNFHelper.launchBeautyEyeLNF();

            }
            else {
                UIManager.setLookAndFeel(UIManager
                        .getSystemLookAndFeelClassName());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Main5(String path)throws HeadlessException{
        super ("Project");

        run();

//        setSize(1200, 800);
    }

    public static void main(final String... args) {
//        initUserInterface();

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                String arg = args.length > 0 ? args[0] : null;
                Main5 main5 = new Main5(arg);

                main5.setDefaultCloseOperation(main5.EXIT_ON_CLOSE);
                main5.setLocationRelativeTo(null);
                main5.setVisible(true);
            }
        });

    }

    public void run(){
        //创建JLabel设置图标  180x50
        ImageIcon icon1 = new ImageIcon("./img/logo.png");
        JLabel label1 = new JLabel(icon1);
        label1.setBounds(160, 35, 180, 50);

        //创建JLabel
        JLabel LBl_Username = new JLabel("用户名：");
        JLabel LBl_Password = new JLabel("密  码：");
        //设置JLabel的字体风格和对齐方式和大小
        LBl_Username.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));
        LBl_Password.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));
        //设置JLabel的位置和大小的两种方式
        LBl_Username.setLocation(140, 130);
        LBl_Username.setSize(60, 25);

        LBl_Password.setBounds(140, 180, 60, 25);
        //创建JPasswordField,JTextField
        final JPasswordField passwordField = new JPasswordField();
        final JTextField textField = new JTextField();

        //创建登录和取消的JButton
        final JButton Login_Btn = new JButton("登录");
        JButton Cancel_Btn = new JButton("取消");

        //设置JButton的大小
        Login_Btn.setBounds(140, 220, 60, 25);
        Cancel_Btn.setBounds(250, 220, 60, 25);

        /**
         * 设置JPasswordField,JTextField的属性
         * */
        //设置位置 和宽度、高度
        passwordField.setBounds(210, 180, 100, 25);
        textField.setBounds(210, 140, 100, 25);

        //设置输入的字体
        textField.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));


        //设置布局管理器
        Container container = this.getContentPane();

        //将组件加入到布局管理器
        container.add(label1);
        container.add(LBl_Username);
        container.add(LBl_Password);
        container.add(passwordField);
        container.add(textField);
        container.add(Login_Btn);
        container.add(Cancel_Btn);

        /**
         * 对按钮附加功能 采用监听事件完成
         * */
        //对用户名输入框限制最大输入为7位
        textField.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {

                if(e.getKeyChar() == KeyEvent.VK_BACK_SPACE){

                    e.isActionKey();

                }else if(textField.getText().length() >8){

                    e.consume();
                }
            }
        });

        //textField点击Enter切换到密码输入框
        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //光标切换到passwordField框上去
                passwordField.requestFocus();
            }
        });

        //passwordField密码框点击enter实现登录
        passwordField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //当鼠标光标在密码框时 点击登录
                Login_Btn.doClick();
            }
        });

        //当登录按钮被点击后 将进行登录操作
        Login_Btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JFrame frame = new JFrame();

                frame.setSize(800, 800);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });

        //设置窗体的Logo
        Image image=Toolkit.getDefaultToolkit().createImage("./img/icon.png");
        this.setIconImage(image);
        //设置窗体的属性
        this.setLayout(null);//设置空布局管理器
        this.setVisible(true);//设置窗体可见
        this.setTitle("组件的学习");//设置窗体标题
        this.setSize(500, 500);//设置窗体的高、宽
        this.setLocationRelativeTo(null);//设置居中显示
        //设置Jframe窗体关闭时 程序结束
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
